package com.wg.user.util;


import com.wg.user.result.ResponseCode;
import com.wg.user.result.ResponseMsg;

import java.util.ArrayList;
import java.util.HashMap;

public final class MyResponse {

    public static HashMap responseData(int code, String type) {
        return responseData(code, "", type);
    }

    public static HashMap responseData(int code, String msg, String type) {
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("code", code);
        responseData.put("data", emptyData(type));
        responseData.put("msg", msg);
        responseData.put("success", code < 1);
        responseData.put("timestamp", System.currentTimeMillis());
        return responseData;
    }

    public static HashMap responseData(Object object, String type) {
        int code = ResponseCode.SUCCESS;
        String msg = ResponseMsg.SUCCESS;
        if (object == null) {
            code = ResponseCode.NOT_EXISTS;
            msg = ResponseMsg.NOT_EXISTS;
        }

        return responseData(object, code, msg, type);
    }

    public static HashMap responseData(Object object, int code, String type) {
        return responseData(object, code, "", type);
    }

    public static HashMap responseData(Object object, int code, String msg, String type) {
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("code", code);
        if (object == null) {
            responseData.put("data", emptyData(type));
        } else {
            responseData.put("data", object);
        }
        responseData.put("msg", msg);
        responseData.put("success", code < 1);
        responseData.put("timestamp", System.currentTimeMillis());

        return responseData;
    }

    @org.jetbrains.annotations.NotNull
    private static Object emptyData(String type) {
        if (type.equals("list")) {
            return new ArrayList<>();
        }

        if (type.equals("map")) {
            return new HashMap<>();
        }

        return null;
    }
}